package com.wsoft.form.manager;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.entity.WfFormColumnEntity;
import com.wsoft.form.mapper.WfFormColumnMapper;
import com.wsoft.form.query.column.ColumnListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/8/29 09:20
 */
@Slf4j
@Service
public class WfFormColumnManager extends ServiceImpl<WfFormColumnMapper, WfFormColumnEntity> {

    @Resource
    private WfFormVirtualColumnManager virtualColumnManager;
    @Resource
    private RedisService redisService;

    /**
     * 查询 tableId 下 是否存在该字段名
     *
     * @param tableId    表id
     * @param columnName 字段名
     * @return WfFormColumnEntity
     */
    public WfFormColumnEntity getByTableIdAndColumnName(Long tableId, String columnName) {
        QueryWrapper<WfFormColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormColumnEntity::getTableId, tableId)
                .eq(WfFormColumnEntity::getColumnName, columnName);
        return getOne(query);
    }

    /**
     * 返回表 所有字段
     *
     * @param tableIdList 表id List
     * @return List
     */
    public List<WfFormColumnEntity> getColumnList(List<Long> tableIdList) {
        List<WfFormColumnEntity> list = new ArrayList<>();
        for (Long tableId : tableIdList) {
            list.addAll(this.getColumnList(tableId));
        }
        return list;
    }

    /**
     * 返回表 所有字段
     *
     * @param tableId 表id
     * @return List
     */
    @GetRedisCache(type = RedisKeyConstant.Form.COLUMN_LIST,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<WfFormColumnEntity> getColumnList(Long tableId) {
        QueryWrapper<WfFormColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormColumnEntity::getTableId, tableId)
                .orderByAsc(WfFormColumnEntity::getColumnSort);
        return list(query);
    }

    /**
     * 根据 tableId 删除
     *
     * @param tableId
     * @return
     */
    public boolean removeByTableId(Long tableId) {
        QueryWrapper<WfFormColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormColumnEntity::getTableId, tableId);
        boolean b = remove(query);
        if (b) {
            redisService.del(RedisKeyConstant.Form.COLUMN_LIST + tableId);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_INFO + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据 formId 删除
     *
     * @param formId formId
     * @return boolean
     */
    public boolean removeByFormId(Long formId) {
        QueryWrapper<WfFormColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormColumnEntity::getFormId, formId);
        boolean b = remove(query);
        if (b) {
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id获取
     *
     * @param columnId
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Form.COLUMN_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormColumnEntity getById(Long columnId) {
        return super.getById(columnId);
    }

    /**
     * 根据id集合获取
     *
     * @param ids
     * @return
     */
    @Override
    public List<WfFormColumnEntity> listByIds(Collection<? extends Serializable> ids) {
        List<WfFormColumnEntity> list = new ArrayList<>();
        for (Serializable id : ids) {
            list.add(this.getById(id));
        }
        return list;
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFormColumnEntity entity) {
        boolean b = super.save(entity);
        if (b) {
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 批量保存
     *
     * @param list
     * @return
     */
    public boolean saveBatch(List<WfFormColumnEntity> list) {
        boolean b = super.saveBatch(list);
        if (b) {
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 跟新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFormColumnEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.COLUMN_INFO + entity.getId());
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 删除
     *
     * @param columnId
     * @return
     */
    public boolean removeById(Long columnId) {
        boolean b = super.removeById(columnId);
        if (b) {
            redisService.del(RedisKeyConstant.Form.COLUMN_INFO + columnId);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.COLUMN_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 获取字段分页
     * @param query
     * @return
     */
    public List<WfFormColumnEntity> listColumn(ColumnListQuery param) {
        QueryWrapper<WfFormColumnEntity> query = new QueryWrapper<>();
        query.lambda().eq(ObjUtil.isNotEmpty(param.getTableId()),WfFormColumnEntity::getTableId,param.getTableId())
                .like(StrUtil.isNotBlank(param.getColumnName()),WfFormColumnEntity::getColumnName,param.getColumnName())
                .eq(StrUtil.isNotBlank(param.getColumnType()),WfFormColumnEntity::getColumnType,param.getColumnType())
                .eq(StrUtil.isNotBlank(param.getFieldKind()),WfFormColumnEntity::getFieldKind,param.getFieldKind())
                .orderByAsc(WfFormColumnEntity::getColumnSort);
        return list(query);
    }
}
